from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin, RetrieveModelMixin
from rest_framework.viewsets import GenericViewSet, ViewSet
from rest_framework.viewsets import ModelViewSet
from django_filters.rest_framework import DjangoFilterBackend
from .pagination import MyPagination

# <<<<<<< HEAD
from user.models import Doctor
from .pagination import MyPagination
from .models import RegisterInfo, DetailedList
from utils.common_response import APIResponse
from rest_framework.filters import SearchFilter
from .reg_filter import MyFilter
from .serializer import RegisterInfoSerializer, ListDetailSerializer


#
#
# class RegisterInfoView(ModelViewSet):
#     queryset = RegisterInfo.objects.all().order_by('id') # print(queryset,'>>>>')
#     serializer_class = RegisterInfoSerializer
#     filter_backends = [MyFilter, ]
#     pagination_class = MyPagination
#
#     def list(self, request, *args, **kwargs):
#         res = super().list(request, *args, **kwargs)
#         # print(res.data)
#         return APIResponse(info_list=res.data)
#
#     def retrieve(self, request, *args, **kwargs):
#         res = super().retrieve(request, *args, **kwargs)
#         return APIResponse(info=res.data)
#
#     def destroy(self, request, *args, **kwargs):
#         res = super().destroy(request, *args, **kwargs)
#         return APIResponse(info=res.data)
# =======
# from rest_framework.viewsets import ModelViewSet,ViewSet
# from .models import *
# from .serializer import *
# from utils.common_response import APIResponse
# from utils.common_page import CommonPagination
# from django_filters.rest_framework import DjangoFilterBackend
#
# # Create your views here.
#
# ########################挂号###############
class RegisterInfoView(ModelViewSet):
    queryset = RegisterInfo.objects.all()
    serializer_class = RegisterInfoSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ('doctor__name',)

    def create(self, request, *args, **kwargs):
        doctor_id = request.data.get('doctor')
        super().create(request, *args, **kwargs)
        doctor_name = Doctor.objects.filter(id=doctor_id).first().name
        return APIResponse(doctor_name=doctor_name, msg='预约成功')


#
#
# <<<<<<< HEAD
# class DetailedListView(GenericViewSet, ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin):
#     queryset = DetailedList.objects.all()
#     serializer_class = DetailedListSerializer
#     pagination_class = MyPagination
# =======
####################缴费列表########################
class ListDetailView(ModelViewSet):
    queryset = DetailedList.objects.all().order_by('id')
    serializer_class = ListDetailSerializer
    pagination_class = MyPagination
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ('patient_name', 'status')


# 更新订单详情
class ModifyStateView(ViewSet):
    def create(self, request):
        DetailedList.objects.filter(id=request.data.get('id')).update(status=request.data.get('status'))
        return APIResponse(msg='订单完成')


############# 药品开单
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin
from rest_framework.viewsets import GenericViewSet, ViewSet
from ending_framework_api.apps.leaveplatform.page import MyPagination
from .serializer import DetailedListSerializer
from rest_framework.decorators import action
from rest_framework.response import Response

# http://127.0.0.1:8000/api/v1/api/v1/registered/detailed_list/post/
class DetailedListView(GenericViewSet, ListModelMixin, CreateModelMixin, DestroyModelMixin):
    queryset = DetailedList.objects.all()
    serializer_class = DetailedListSerializer
    # # 分页功能
    # pagination_class = MyPagination
    @action(methods=['GET'], detail=False)
    def post(self, request, *args, **kwargs):
        print(request.data)
        ser = self.get_serializer(data=request.data)  # # 相当于序列化类
        if ser.is_valid():
            print(ser.validated_data)  # 打印校验之后的数据
            ser.save()
            # return Response({'status': 100, 'msg': '新增数据成功', "data": ser.validated_data})
            return Response({'status': 100, 'msg': '新增数据成功'})
        else:
            return Response({'status': 100, 'msg': '新增失败', "data": ser.errors})
